package com.jinzhi.jzsynthesize.controller;


import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.ExaminationBatchDO;
import com.jinzhi.jzweb.domain.ExaminationResultsDO;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.domain.StuSignUpDO;
import com.jinzhi.jzweb.service.ExaminationBatchService;
import com.jinzhi.jzweb.service.ExaminationResultsService;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.StuSignUpService;
import com.jinzhi.sys.domain.UserDO;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@AllArgsConstructor
@RequestMapping("/synthesize/analyze")
public class ResultAnalyzeController extends BaseController {

	@Autowired
	private ExaminationBatchService examinationBatchService;

	@Autowired
	private ExaminationResultsService examinationResultsService;

	@Autowired
	private OrganizationNewsService organizationNewsService;

	@Autowired
	private StuSignUpService stuSignUpService;

	@GetMapping()
	@RequiresPermissions("synthesizeWeb:analyze:analyze")
	String analyze(){
		return "synthesizeWeb/analyze/analyze";
	}

	@Log("成绩分析接口（无用）")
	@PostMapping("/analyze1")
	@ResponseBody
	Result<?> findAnalyze(HttpServletRequest req ,String eBatchId) {

		if (eBatchId.equals("")) {
			eBatchId = "0";
		}

		Object orgId = req.getSession().getAttribute("orgId");
		OrganizationNewsDO org = new OrganizationNewsDO();
		if (orgId != null){
			org = organizationNewsService.selectById(orgId.toString());
		}else {
			UserDO user = this.getUser();
			//根据用户所属机构查询机构信息
			org = organizationNewsService.selectById(user.getOrgId());
		}

		List reList = new ArrayList();
		ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(Long.parseLong(eBatchId));

		List<String> sList = Arrays.asList("北京市","天津市","上海市","重庆市","河北省","河南省","云南省","辽宁省","黑龙江省","湖南省","安徽省","山东省","新疆维吾尔自治区",
				"江苏省","浙江省","江西省","湖北省","广西壮族自治区","甘肃省","山西省","内蒙古自治区","陕西省","吉林省","福建省","贵州省","广东省","青海省","西藏自治区","四川省",
				"宁夏回族自治区","海南省","台湾省","香港特别行政区","澳门特别行政区");

		Map map = new HashMap();
		Double maxScore = 0.00;//定义一个最高分
		Double avg = 0.00; //定义一个平均分
		Double pro = 0.00; //定义一个通过率
		String province = "";  //定义一个省份

		for (int i = 0; i < sList.size(); i++) {
			if (org.getProvince().equals(sList.get(i))){
				//省份所有考点id
				List<Long> orgIds = organizationNewsService.selOrgProvince(sList.get(i));
				if (orgIds != null && orgIds.size() > 0) {
					for (int j = 0; j < orgIds.size(); j++) {
						if (examinationBatchDO != null) {
							//考点下该认证批次所有考生
							List<StuSignUpDO> stuSignUpDOS = stuSignUpService.findByKv("org_id", orgIds.get(j), "cbatch_id", examinationBatchDO.getCbatchId());
							if (stuSignUpDOS != null && stuSignUpDOS.size() > 0) {
								Double total = 0.00; //定义成绩总和
								Double pass = 0.00;  //定义通过人数
//							List<ExaminationResultsDO> eResult = examinationResultsService.findByKv("ebatch_id" , eBatchId);
								for (int n = 0; n < stuSignUpDOS.size(); n++) {
									//查询考生对应的考试成绩
									ExaminationResultsDO examinationResultsDO = examinationResultsService.findOneByKv("cbatch_id", examinationBatchDO.getCbatchId(), "ebatch_id", examinationBatchDO.getId(), "stu_sign_id", stuSignUpDOS.get(n).getUserId());
									if (examinationResultsDO != null) {
										//查询最高分
										if (examinationResultsDO.getTotalScore() > maxScore) {
											maxScore = examinationResultsDO.getTotalScore();
										}
										//查询计算平均分
										total = total + examinationResultsDO.getTotalScore();
										avg = total / stuSignUpDOS.size();

										//成绩分数大于合格分数 通过人数+1
										if (examinationResultsDO.getTotalScore() >= examinationBatchDO.getPassMark()){
											pass = pass + 1;
										}
										//计算通过率
										pro = (pass / stuSignUpDOS.size())*100;

									}
								}
							}
						}
					}
				}
				province = sList.get(i);
				map.put("max", maxScore);//最高分
				map.put("avg" , avg); //平均分
				map.put("ps" , pro);  //通过率
				map.put("province" , province); //省份
				reList.add(map);
			}
		}
		return Result.ok(reList);
	}



	@Log("成绩分析接口")
	@PostMapping("/analyze")
	@ResponseBody
	Result<?> findAnalyzes(HttpServletRequest req ,String eBatchId) {

		if (eBatchId.equals("")) {
			eBatchId = "0";
		}

		/*Object orgId = req.getSession().getAttribute("orgId");
		OrganizationNewsDO org = new OrganizationNewsDO();
		if (orgId != null){
			org = organizationNewsService.selectById(orgId.toString());
		}else {
			UserDO user = this.getUser();
			//根据用户所属机构查询机构信息
			org = organizationNewsService.selectById(user.getOrgId());
		}*/

		List reList = new ArrayList();
		ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(Long.parseLong(eBatchId));

		List<String> sList = Arrays.asList("北京市","天津市","上海市","重庆市","河北省","河南省","云南省","辽宁省","黑龙江省","湖南省","安徽省","山东省","新疆维吾尔自治区",
				"江苏省","浙江省","江西省","湖北省","广西壮族自治区","甘肃省","山西省","内蒙古自治区","陕西省","吉林省","福建省","贵州省","广东省","青海省","西藏自治区","四川省",
				"宁夏回族自治区","海南省","台湾省","香港特别行政区","澳门特别行政区");

		//todo 用来存分数和通过率
		List maxScoreList = new ArrayList();
		List avgList = new ArrayList();
		List proList = new ArrayList();

		for (int i = 0; i < sList.size(); i++) {
			//省份所有考点id
			Double maxScore = 0.00;//定义一个最高分
			Double avg = 0.00; //定义一个平均分
			Double pro = 0.00; //定义一个通过率
			List<Long> orgIds = organizationNewsService.selOrgProvince(sList.get(i));
			if (orgIds != null && orgIds.size() > 0) {
				for (int j = 0; j < orgIds.size(); j++) {
					//考点下该认证批次所有考生
					List<StuSignUpDO> stuSignUpDOS = stuSignUpService.findByKv("org_id", orgIds.get(j), "cbatch_id", examinationBatchDO.getCbatchId());
					if (stuSignUpDOS != null && stuSignUpDOS.size() > 0) {
						Double total = 0.00; //定义成绩总和
						Double pass = 0.00;  //定义通过人数
						for (int n = 0; n < stuSignUpDOS.size(); n++) {
							//查询考生对应的考试成绩
							ExaminationResultsDO examinationResultsDO = examinationResultsService.findOneByKv("cbatch_id", examinationBatchDO.getCbatchId(), "ebatch_id", examinationBatchDO.getId(), "stu_sign_id", stuSignUpDOS.get(n).getId());
							if (examinationResultsDO != null) {
								//查询最高分
								if (examinationResultsDO.getTotalScore() > maxScore) {
									maxScore = examinationResultsDO.getTotalScore();
								}

								//查询计算平均分
								total = total + examinationResultsDO.getTotalScore();
								avg = total / stuSignUpDOS.size();

								//成绩分数大于合格分数 通过人数+1
								if (examinationResultsDO.getTotalScore() >= examinationBatchDO.getPassMark()){
									pass = pass + 1;
								}
								//计算通过率
								pro = (pass / stuSignUpDOS.size())*100;

							}

						}

					}

				}

			}
			maxScoreList.add(maxScore);
			avgList.add(avg);
			proList.add(pro);
		}
		Map map = new HashMap();
		map.put("max", maxScoreList);//最高分
		map.put("avg" , avgList); //平均分
		map.put("ps" , proList);  //通过率
		map.put("province" , sList); //省份
		reList.add(map);
		return Result.ok(reList);
	}

}
